<?php
namespace app\friend\controller;
use think\Controller;
use think\Session;
use think\Db;

class Index extends Controller
{
    public function index()
    {
        $id = Session::get('id');
        // var_dump($id);
        //当前用户信息
        $user = Db::table('user_info')->where('uid',$id)->find();
        // var_dump($user);

        //当前用户全部好友
        // $friends = Db::table('friend')->alias('f')->join('user_info u','u.uid = f.fid')->where('f.uid',$id)->select();
        $friends = Db::table('user_info')->where('uid','neq',$id)->select();
        // var_dump($friends);

        $this->assign('user',$user);
        $this->assign('friends',$friends);

        return $this->fetch('index/friends');
    }

    public function see()
    {
        $uid = Session::get('id');
        $id = input('uid');
        // var_dump($id);
        $list = Db::table('user_info')->where('uid',$uid)->find();
        $list1 = Db::table('user')->where('id',$id)->find();
            $list2 = Db::table('user_info')->where('uid',$id)->find();
            $this->assign('title','个人资料');
            $this->assign('list',$list);
            $this->assign('list1',$list1);
            $this->assign('list2',$list2);
            $this->assign('id',$id);
            // var_dump($list1);
            // var_dump($list2);
            return $this->fetch('index/frienddata');
    }

    public function makefriend()
    {
        $id = Session::get('id');
        $uid = input('uid');
        var_dump($uid);
        //添加好友,在friend表中插入信息
        $m = Session::get('mid','friend');
        $y = Session::get('yid','friend');

        if($id == $m && $uid == $y){
            $this->success('请求已发送...');
        } else {
            Db::table('friend')->insert(['id'=>null,'uid'=>$id,'fid'=>$uid,'status'=>0]);
            Db::table('friend')->insert(['id'=>null,'uid'=>$uid,'fid'=>$id,'status'=>0]);
            Session::set('mid',$id,'friend');
            Session::set('yid',$uid,'friend');
            $this->success('请求成功,正在等待对方确认...','index/index');
        }


        
    }
}
